home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Sources / UTheDebugger.cp < prev   
Encoding:
Text File  |  1996-04-03  |  8.8 KB  |  329 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UTheDebugger.cp
  3. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UTHEDEBUGGER__
  7. #include "UTheDebugger.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UOBJECT__
  13. #include "UObject.h"
  14. #endif
  15.  
  16. #ifndef __UCOREUTILITIES__
  17. #include "UCoreUtilities.h"
  18. #endif
  19.  
  20.  
  21. //========================================================================================
  22. // GLOBAL Procedures
  23. //========================================================================================
  24.  
  25. #if qDebug || qTheDebugger
  26.  
  27. static Boolean TheDbgr_CallBack();
  28.  
  29. #endif
  30.  
  31. //========================================================================================
  32. // GLOBAL Procedures
  33. //========================================================================================
  34. #undef Inherited
  35.  
  36. #if qTheDebugger
  37.  
  38. //========================================================================================
  39. // Support for Jasik's The Debugger
  40. //========================================================================================
  41.  
  42. T_ExtDbgr gExtDbgr_Info;        // define struct block for Jasik Dbgr
  43.  
  44. const long c_MA_Objs = 512;        // obj list collector increment size
  45.  
  46.  
  47. //----------------------------------------------------------------------------------------
  48. // TheDbgr_Add_Object:
  49. //----------------------------------------------------------------------------------------
  50. #pragma segment MADebugger
  51.  
  52. void TheDbgr_Add_Object(TObject* objHdl)
  53. {
  54.     long        i,si;
  55.     T_ObjList*    obj_list;
  56.     
  57.     if( !gExtDbgr_Info.hMA_OBJ_List ) return;
  58.     
  59. lp:    {
  60.         obj_list = *gExtDbgr_Info.hMA_OBJ_List;
  61.         if( obj_list->L_used == obj_list->max_objs )
  62.         {
  63.             if( obj_list->n_holes > 0 )     // squeeze out empty entries
  64.             {
  65.                 obj_list->n_holes = 0;
  66.                 si = 0;
  67.                 for(i = 0; i < obj_list->L_used; i++ )
  68.                     if( obj_list->objs[i] != 0 )
  69.                     {
  70.                         obj_list->objs[si++] = obj_list->objs[i];
  71.                     }
  72.                 obj_list->L_used = si;
  73.             }
  74.             else
  75.             {
  76.                 obj_list->max_objs = obj_list->max_objs + c_MA_Objs;
  77.                 SetHandleSize((Handle)gExtDbgr_Info.hMA_OBJ_List, 12 + 4*obj_list->max_objs );
  78.                 obj_list = *gExtDbgr_Info.hMA_OBJ_List;
  79.                 if (MemError() != noErr) {
  80.                     DebugStr((StringPtr)"\pObject Collector could NOT get space to expand OBJ List");
  81.                     return;
  82.                 };
  83.                 goto lp;
  84.             }
  85.         
  86.         }
  87.         obj_list->objs[obj_list->L_used] = objHdl;
  88.         obj_list->L_used++;
  89.     }
  90. }
  91.  
  92. //----------------------------------------------------------------------------------------
  93. // TheDbgr_Delete_Object:
  94. //----------------------------------------------------------------------------------------
  95. #pragma segment MADebugger
  96.  
  97. void TheDbgr_Delete_Object(TObject* objHdl)
  98. {
  99.     long    i;
  100.     T_ObjList*    obj_list;
  101.  
  102.     if( gExtDbgr_Info.hMA_OBJ_List )
  103.     {
  104.         obj_list = *gExtDbgr_Info.hMA_OBJ_List;
  105.  
  106.         for(i = 0; i < obj_list->L_used; i++ )
  107.             if( obj_list->objs[i] == objHdl )
  108.             {
  109.                 if( i == obj_list->L_used - 1 )
  110.                 {
  111.                     obj_list->L_used--;
  112.                 }
  113.                 else
  114.                 {
  115.                     obj_list->objs[i] = 0;  obj_list->n_holes++;
  116.                 }
  117.                 return;
  118.             }
  119.         DebugStr((StringPtr)"\pTheDbgr_Delete_Object - Object not found");
  120.     }
  121. }
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // TheDbgr_CallBack:
  125. //----------------------------------------------------------------------------------------
  126. #pragma segment MADebugger
  127.  
  128. Boolean TheDbgr_CallBack()
  129. {
  130.     long    ConsWR_Proc;
  131.  
  132. #if defined(powerc) || defined (__powerc)
  133.  
  134.     short proc_code[] = {  // adj stk and Call Dbgr
  135.         0x201F,    //    move.l    (SP)+,D0    ; rtn addr
  136.         0x4267,    //    CLR    -(SP)        ; setup Excpt stk frame
  137.         0x2F00,    //    move.l    D0,-(SP)
  138.         0x4267,    //    CLR    -(SP)
  139.         0x7007,    //    MOVEQ    #7,D0 - isDbgrTask code
  140.         0x2078, 0x009C,    //    MOVE.L    4*(32+7),A0    ; trap #7 Handler addr
  141.         0x4ED0    //    JMP    (A0)
  142.     };
  143.  
  144.     enum {
  145.     TD_ProcInfo = kPascalStackBased
  146.          | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  147.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Ptr)))
  148.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Ptr)))
  149. };
  150.     Boolean rtnVal;
  151.     UniversalProcPtr upp = NewRoutineDescriptor((ProcPtr)proc_code , TD_ProcInfo , kM68kISA );
  152.  
  153.     rtnVal = CallUniversalProc(upp,TD_ProcInfo,&gExtDbgr_Info,&ConsWR_Proc);
  154.     DisposeRoutineDescriptor(upp);
  155.     return( rtnVal );
  156. #else
  157.     return( TheDbgr_IsDbgrTask(&gExtDbgr_Info,&ConsWR_Proc) );
  158. #endif
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // TheDbgr_Init_Ext_Dbgr:
  163. //----------------------------------------------------------------------------------------
  164. #pragma segment MADebugger
  165.  
  166. void TheDbgr_Init_Ext_Dbgr()
  167. {    
  168.     // initialize remaining fields in gExtDbgr_Info:
  169.     // note that n_classes and pOrderedClass have already
  170.     // been set in ClassDesc::InitUClassDesc; all other
  171.     // fields were zeroed at global initialization time…
  172.     gExtDbgr_Info.isBeingDebugged = FALSE;
  173.     gExtDbgr_Info.MA_Version = 3;
  174.     
  175.     if( TheDbgr_CallBack() )
  176.     {
  177.         gExtDbgr_Info.isBeingDebugged = TRUE;
  178.  
  179.         // allocate space for an Object collection LIst
  180.  
  181.         gExtDbgr_Info.hMA_OBJ_List = (T_ObjList**) NewHandleClear((3*sizeof(long))+(c_MA_Objs*sizeof(TObject*)));
  182.         
  183.         if( gExtDbgr_Info.hMA_OBJ_List )
  184.         {
  185.             (**gExtDbgr_Info.hMA_OBJ_List).max_objs = c_MA_Objs;
  186.         }
  187.     }
  188. }
  189.  
  190. //    code to sort the class Descriptors so that the Class ID's have the property
  191. //    that TObject is 1st and the class ID of a subclass > class ID of parent
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // sort4:
  195. //----------------------------------------------------------------------------------------
  196. #pragma segment MADebugger
  197.  
  198. static void sort4(long * v, short n)        // Shell Sort from H & S 2nd Edition
  199. {
  200.     short gap,i,j;
  201.     long temp;
  202.     gap = 1;
  203.     do ( gap = 3*gap+1); while (gap <= n);
  204.     for( gap /= 3; gap > 0; gap /= 3 )
  205.     {
  206.         for( i = gap; i < n; i++ )
  207.         {
  208.             temp = v[i];
  209.             for( j = i-gap; (j >= 0) && (v[j] > temp); j -= gap)
  210.                 v[j+gap] = v[j];
  211.             v[j+gap] = temp;
  212.         }
  213.     }
  214. }
  215.  
  216. typedef struct T_cl_Sort
  217. {
  218.     short Cl_ID;
  219.     short subCl_ID;
  220. };
  221.  
  222. static    ClassDesc** pCL_AT;    // ptr to the array
  223. static    short* pCL_IT;
  224. static    T_cl_Sort*    psubc_IT = NULL;
  225.  
  226. //static    short    g_Lvl = 0;
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // follow:
  230. //----------------------------------------------------------------------------------------
  231. #pragma segment MAInit
  232.  
  233. static void follow(short f_id)
  234. {
  235.     static    short    new_CL_ID = 0;
  236.     
  237.     ClassDesc* pCD = pCL_AT[f_id];
  238.     if( pCD )
  239.     {
  240.         pCL_AT[f_id] = 0;
  241.         pCD->SetClassID(++new_CL_ID);    // assign a new Class ID
  242.     
  243.         short indx = pCL_IT[f_id];        // index to sub class List
  244.         //g_Lvl = g_Lvl + 1;
  245.         while( (indx >= 0) && (psubc_IT[indx].Cl_ID == f_id) )
  246.         {
  247.             follow( psubc_IT[indx].subCl_ID );
  248.             indx--;
  249.         }
  250.         //g_Lvl = g_Lvl + 1;    // •• TWB •• probably should be -1
  251.     }
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. // TheDbgr_Adjust_ClassDescr_Ids:
  256. //----------------------------------------------------------------------------------------
  257. #pragma segment MADebugger
  258.  
  259. void TheDbgr_Adjust_ClassDescr_Ids(ClassDesc* pclassList, short numClasses)
  260. {
  261.     numClasses++;    // for safety
  262.     pCL_IT     = (short*)         NewPtrClear(sizeof(short) * numClasses);        // point to arrays that follow accesses
  263.     psubc_IT = (T_cl_Sort*)  NewPtrClear(sizeof(T_cl_Sort) * numClasses);
  264.     pCL_AT     = (ClassDesc**) NewPtrClear(sizeof(ClassDesc*) * numClasses);
  265.     
  266.     short *CL_objNum = (short*)  NewPtrClear(sizeof(short) * numClasses);   // < FIX
  267.  
  268.     if (pCL_IT && psubc_IT && pCL_AT)    // check our allocations
  269.     {
  270.         short i;
  271.         short n_cl = 0;
  272.         short n_objNum = 0; // <== FIX
  273.         short CL_Tobj = 0;
  274.         
  275.         ClassDesc* pCD = pclassList;
  276.         while( pCD )
  277.         {
  278.             psubc_IT[n_cl].subCl_ID = i = pCD->GetClassID();        // id of this class is sub Class ID
  279.             pCL_AT[i] = pCD;
  280.             if( pCD->CountBaseClasses() > 0 )    // MacApp 3.3 can't do GetBaseClass(1) if the count is 0
  281.             {
  282.                 const ClassDesc* pBC = pCD->GetBaseClass(1);
  283.                 psubc_IT[n_cl].Cl_ID = pBC->GetClassID();
  284.             }
  285.             else
  286.             {
  287.                 if (pCD == TObject::GetClassDescStatic())
  288.                     CL_Tobj = i;
  289.                 else                            // <== FIX
  290.                     CL_objNum[n_objNum++] = i;  // <== FIX
  291.  
  292.                 psubc_IT[n_cl].Cl_ID = 0;
  293.             }
  294.             pCD = (ClassDesc*) pCD->GetNextClassDesc();        // const cast away
  295.             n_cl++;
  296.         }
  297.     
  298.         sort4( (long *) &(psubc_IT[0]) , n_cl );
  299.         psubc_IT[n_cl].Cl_ID = -1;        // terminator for loop in follow
  300.         
  301.         for( i = 0; i <= n_cl; i++ ) pCL_IT[i] = 0;
  302.         
  303.         short last_cl = psubc_IT[0].Cl_ID;
  304.         for ( i = 0; i <= n_cl; i++ )
  305.         {
  306.             short j = psubc_IT[i].Cl_ID;
  307.             if( j != last_cl )
  308.             {
  309.                 pCL_IT[last_cl] = i - 1;
  310.                 last_cl = j;
  311.             }
  312.         }
  313.         follow(CL_Tobj);
  314.         for ( i = 0; i < n_objNum; i++) follow(CL_objNum[i]);   // <== FIX
  315.     }
  316.  
  317.     DisposeIfPtr((Ptr)pCL_IT);        // point to arrays that follow accesses
  318.     DisposeIfPtr((Ptr)psubc_IT);
  319.     DisposeIfPtr((Ptr)pCL_AT);
  320.     DisposeIfPtr((Ptr)CL_objNum);    // <== FIX
  321. }
  322.  
  323. #endif    // #if qTheDebugger
  324.  
  325. //----------------------------------------------------------------------------------------
  326. // End of UTheDebugger.cp
  327.  
  328. #pragma segment Inline
  329.